package com.appsindy.common.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.AppsindyConstants;
import com.appsindy.common.json.JSONResultCommon;
import com.appsindy.reserv.bo.Combo;
import com.appsindy.reserv.service.ReservationServices;
import com.appsindy.reserv.vo.DiscountVO;
import com.appsindy.reserv.vo.GroupInfoVO;
import com.appsindy.reserv.vo.ReserveDetailVO;
import com.appsindy.reserv.vo.ReservePaymentDetailVO;
import com.appsindy.reserv.vo.ReservePaymentVO;
import com.opensymphony.xwork2.Action;

public class ReservePaymentAction extends AppsindyBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private ReservationServices reservationServices;
	
	private int shiftTransId;
	private int groupId;
	private int serviceHeaderId;
	private int serviceDetailId;
	private int servicePaymentId;
	private int servicePaymentDetailId;
	private String arrServiceDetailId;
	
	private ReservePaymentVO reservePaymentVO;
	private DiscountVO discountVO;
	
	private List<Combo> paymentMethodList;
	private List<Combo> roomList;

	private String[] colPaymentMethodId;
	private String[] colPaymentMethodName;
	private String[] colRefNo;
	private String[] colServiceDetailId;
	private String[] colDateFromTo;
	private String[] colCustomerName;
	private String[] colAmount;
	private int[] colServicePaymentDetailId;
	private int[] colShiftTransId;
	
	private String flagSave;
	private String flagConfirm;
	
	public String execute() {
		log.info("Init Reservation Payment ...");

		if(reservePaymentVO == null) reservePaymentVO = new ReservePaymentVO();
		if(serviceHeaderId > 0 && groupId > 0){
			reservePaymentVO.setServiceHeaderId(serviceHeaderId);
			GroupInfoVO groupInfoVO = new GroupInfoVO();
			groupInfoVO.setGroupId(groupId);
			reservePaymentVO.setGroupInfoVO(groupInfoVO);
			reservePaymentVO.setArrServiceDetailId(arrServiceDetailId);
		}		
		try{
			//set default shift trans id
			shiftTransId = (Integer)getSession().get(AppsindyConstants.S_SHIFT_TRANS_ID);
		}catch(Exception e){
			e.printStackTrace();
		}

		return initialReserve();
	}
	
	private String initialReserve(){
		log.info("Initial Reservation Information ...");		
		setPaymentMethodList(getReservationServices().getPaymentMethodCombos());
		
		if(getServiceHeaderId() > 0){
			setRoomList(getReservationServices().getRoomByHeaderCombos(getServiceHeaderId()));		
			reservePaymentVO = getReservationServices().selectReservePayment(getReservePaymentVO());
		}
		return SUCCESS;
	}
	
	public String newDiscount() {
		log.info("newDiscount Popup ...");
		return "discountPopup";
	}
	
//	public String addDiscount() {
//		log.info("addDiscount ...");
//		
//		discountVO = getReservationServices().checkAuthorizedDiscount(discountVO);
//		if(discountVO != null){
//			if("N".equalsIgnoreCase(discountVO.getAuthorizedFlag())){
//				addActionError(getText("reserve.error.noauthorized4discount"));
//			}
//		}
//
//		return newDiscount();
//	}	

    @SuppressWarnings("finally")
	public String saveDiscountJSON(){
    	log.info("saveDiscountJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't check discount");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	discountVO = getReservationServices().checkAuthorizedDiscount(discountVO);
        	
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, discountVO);
        } catch(Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }
    
	private void manageDetailList() throws Exception{
		log.info("manageDetailList ...");
        List<ReservePaymentDetailVO> resultList = new ArrayList<ReservePaymentDetailVO>();
        if(colAmount != null && colAmount.length > 0){
            int size = colAmount.length;
            ReservePaymentDetailVO vo = null;
            for(int i=0; i<size; i++){
                vo = new ReservePaymentDetailVO();
                vo.setPaymentMethodId(colPaymentMethodId[i]);
                vo.setPaymentMethodName(colPaymentMethodName[i]);
                vo.setRefNo(colRefNo[i]);
                vo.setServiceDetailId(colServiceDetailId[i]);
                //vo.setDateFromTo(colDateFromTo[i]);
                vo.setCustomerName(colCustomerName[i]);
                vo.setAmount(colAmount[i]);
                vo.setServicePaymentDetailId(colServicePaymentDetailId[i]);
                vo.setShiftTransId(colShiftTransId[i]);
                resultList.add(vo);
            }
        }
        getReservePaymentVO().setPaymentMethodList(resultList);
	}
	
	public String save(){
		log.info("Save Reserve Payment ...");
		try{
			manageDetailList();
		} catch(Exception e){ }
		/*
		List<ReservePaymentDetailVO> list = getReservePaymentVO().getPaymentMethodList();
		if(list != null){
			int size = list.size();
			for(int i=0; i<size; i++){
				ReservePaymentDetailVO vo = list.get(i);
				log.info("[save " + (i+1) + "] getServicePaymentDetailId = " + vo.getServicePaymentDetailId() + " , PaymentMethodName = " + vo.getPaymentMethodName());
			}
		}
		*/
		reservePaymentVO = getReservationServices().saveReservePayment(getReservePaymentVO());
		if(reservePaymentVO != null && reservePaymentVO.getServiceHeaderId() > 0){
			groupId 		= reservePaymentVO.getGroupInfoVO().getGroupId();
			serviceHeaderId = reservePaymentVO.getServiceHeaderId();
		}
		flagSave = "Y";
		return initialReserve();
	}

	public String confirm(){
		log.info("Confirm Reserve Payment...");
		try{
			manageDetailList();
		} catch(Exception e){ }
		
		reservePaymentVO = getReservationServices().confirmReservePayment(getReservePaymentVO());
		if(reservePaymentVO != null && reservePaymentVO.getServiceHeaderId() > 0){
			groupId = reservePaymentVO.getGroupInfoVO().getGroupId();
			serviceHeaderId = reservePaymentVO.getServiceHeaderId();
			servicePaymentId = reservePaymentVO.getServicePaymentId();
		}
		flagConfirm = "Y";
		return initialReserve();
	}

    @SuppressWarnings("finally")
	public String getRoomInfoJSON(){
    	log.info("getRoomRateJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Cannot get Room Info");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	ReserveDetailVO infoVO = getReservationServices().getRoomInfo(serviceDetailId);
        	
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, infoVO);
        } catch(Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }
    
	public ReservationServices getReservationServices() {
		return reservationServices;
	}
	public void setReservationServices(ReservationServices reservationServices) {
		this.reservationServices = reservationServices;
	}

	public ReservePaymentVO getReservePaymentVO() {
		return reservePaymentVO;
	}
	public void setReservePaymentVO(ReservePaymentVO reservePaymentVO) {
		this.reservePaymentVO = reservePaymentVO;
	}

	public int getGroupId() {
		return groupId;
	}
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public int getServiceHeaderId() {
		return serviceHeaderId;
	}
	public void setServiceHeaderId(int serviceHeaderId) {
		this.serviceHeaderId = serviceHeaderId;
	}

	public List<Combo> getPaymentMethodList() {
		return paymentMethodList;
	}
	public void setPaymentMethodList(List<Combo> paymentMethodList) {
		this.paymentMethodList = paymentMethodList;
	}

	public List<Combo> getRoomList() {
		return roomList;
	}
	public void setRoomList(List<Combo> roomList) {
		this.roomList = roomList;
	}

	public int getServiceDetailId() {
		return serviceDetailId;
	}
	public void setServiceDetailId(int serviceDetailId) {
		this.serviceDetailId = serviceDetailId;
	}
	
	public DiscountVO getDiscountVO() {
		return discountVO;
	}
	public void setDiscountVO(DiscountVO discountVO) {
		this.discountVO = discountVO;
	}
	
	public String[] getColPaymentMethodId() {
		return colPaymentMethodId;
	}
	public void setColPaymentMethodId(String[] colPaymentMethodId) {
		this.colPaymentMethodId = colPaymentMethodId;
	}

	public String[] getColRefNo() {
		return colRefNo;
	}
	public void setColRefNo(String[] colRefNo) {
		this.colRefNo = colRefNo;
	}

	public String[] getColServiceDetailId() {
		return colServiceDetailId;
	}
	public void setColServiceDetailId(String[] colServiceDetailId) {
		this.colServiceDetailId = colServiceDetailId;
	}

	public String[] getColDateFromTo() {
		return colDateFromTo;
	}
	public void setColDateFromTo(String[] colDateFromTo) {
		this.colDateFromTo = colDateFromTo;
	}

	public String[] getColCustomerName() {
		return colCustomerName;
	}
	public void setColCustomerName(String[] colCustomerName) {
		this.colCustomerName = colCustomerName;
	}

	public String[] getColAmount() {
		return colAmount;
	}
	public void setColAmount(String[] colAmount) {
		this.colAmount = colAmount;
	}

	public String getArrServiceDetailId() {
		return arrServiceDetailId;
	}
	public void setArrServiceDetailId(String arrServiceDetailId) {
		this.arrServiceDetailId = arrServiceDetailId;
	}

	public String getFlagSave() {
		return flagSave;
	}
	public void setFlagSave(String flagSave) {
		this.flagSave = flagSave;
	}

	public String getFlagConfirm() {
		return flagConfirm;
	}
	public void setFlagConfirm(String flagConfirm) {
		this.flagConfirm = flagConfirm;
	}

	public String[] getColPaymentMethodName() {
		return colPaymentMethodName;
	}
	public void setColPaymentMethodName(String[] colPaymentMethodName) {
		this.colPaymentMethodName = colPaymentMethodName;
	}

	public int getServicePaymentId() {
		return servicePaymentId;
	}
	public void setServicePaymentId(int servicePaymentId) {
		this.servicePaymentId = servicePaymentId;
	}

	public int[] getColServicePaymentDetailId() {
		return colServicePaymentDetailId;
	}

	public void setColServicePaymentDetailId(int[] colServicePaymentDetailId) {
		this.colServicePaymentDetailId = colServicePaymentDetailId;
	}

	public int getServicePaymentDetailId() {
		return servicePaymentDetailId;
	}

	public void setServicePaymentDetailId(int servicePaymentDetailId) {
		this.servicePaymentDetailId = servicePaymentDetailId;
	}

	public int[] getColShiftTransId() {
		return colShiftTransId;
	}

	public void setColShiftTransId(int[] colShiftTransId) {
		this.colShiftTransId = colShiftTransId;
	}

	public int getShiftTransId() {
		return shiftTransId;
	}

	public void setShiftTransId(int shiftTransId) {
		this.shiftTransId = shiftTransId;
	}
    
}
